#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import os
import os.path
from tools.scons.RunTest import run_test

Import('test_env')

# SConscript file for Local PKI google tests
provisiontests_env = test_env.Clone()
src_dir = provisiontests_env.get('SRC_DIR')
target_os = provisiontests_env.get('TARGET_OS')
with_ra = provisiontests_env.get('WITH_RA')
with_ra_ibb = provisiontests_env.get('WITH_RA_IBB')

######################################################################
# Build flags
######################################################################
provisiontests_env.PrependUnique(CPPPATH=[
    '#/resource/include',
    '#/resource/c_common',
    '#/resource/c_common/oic_malloc/include',
    '#/resource/c_common/oic_string/include',
    '#/resource/oc_logger/include',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/security/include',
    '#/resource/csdk/security/provisioning/include',
    '#/resource/csdk/security/provisioning/include/oxm',
    '#/resource/csdk/security/provisioning/include/internal',
    '#/resource/csdk/security/provisioning/include/cloud',
    '#/resource/csdk/stack/include/internal',
    '#/resource/csdk/connectivity/api',
    '#/resource/csdk/connectivity/inc/pkix',
    '#/resource/csdk/connectivity/external/inc',
    '#/resource/csdk/ocsocket/include',
    '#/resource/csdk/ocrandom/include',
    '#/resource/csdk/logger/include',
    '#/resource/csdk/connectivity/lib/libcoap-4.1.1/include',
    '#/extlibs/hippomocks/hippomocks',
    ])

if target_os in ['windows']:
    # Non-Windows platforms find the boost headers under /usr/include or similar path.
    provisiontests_env.AppendUnique(CPPPATH=['#/extlibs/boost/boost'])
    # C4996 Using deprecated code warning. We still test deprecated functions. 
    provisiontests_env.AppendUnique(CCFLAGS=['/wd4996'])

    provisiontests_env.AppendUnique(LIBS=['sqlite3', 'win_helper'])
elif target_os in ['tizen', 'linux']:
    # We still test deprecated functions.
    provisiontests_env.AppendUnique(CCFLAGS=['-Wno-deprecated-declarations'])
    provisiontests_env.ParseConfig('pkg-config --cflags --libs sqlite3 gobject-2.0 gio-2.0 glib-2.0')

# build fix: don't use _internal unless have to
if target_os in ('msys_nt', 'windows'):
    calib = 'connectivity_abstraction_internal'
else:
    calib = 'connectivity_abstraction'

provisiontests_env.PrependUnique(LIBS=[
    'ocprovision',
    'oc',
    'ocpmapi_internal',
    'octbstack_internal',
    'routingmanager',
    'oc_logger_internal',
    'logger',
    calib,
    'coap',
    'ocsrm',
    'routingmanager',
    'c_common',
])

if with_ra_ibb:
    provisiontests_env.AppendUnique(
        LIBS=provisiontests_env['RALIBS'],
        LIBPATH=provisiontests_env['RALIBPATH'],
        RPATH=provisiontests_env['RARPATH'])
    print("RALIBS = %s" % provisiontests_env['RALIBS'])
    print("RALIBPATH = %s" % provisiontests_env['RALIBPATH'])
    print("RARPATH = %s" % provisiontests_env['RARPATH'])
else:
    if with_ra:
        provisiontests_env.AppendUnique(LIBS=['ra_xmpp'])

if provisiontests_env.get('SECURED') == '1':
    provisiontests_env.AppendUnique(LIBS=[
        'mbedtls',
        'mbedx509',
        'mbedcrypto'
    ])

######################################################################
# Source files and Targets
######################################################################
ocprovisiontest_src = [
    'OCProvisioningTest.cpp'
]

if provisiontests_env.get('WITH_TCP') == True and provisiontests_env.get('WITH_CLOUD') == True:
    ocprovisiontest_src.append('OCCloudProvisioningTest.cpp')

unittests = provisiontests_env.Program('provisiontests', ocprovisiontest_src)

Alias("provisiontests", [unittests])

provisiontests_env.AppendTarget('provisiontests')
if provisiontests_env.get('TEST') == '1':
    # Workaround for IOT-2038: don't run this test on Windows yet, because Jenkins times out.
    if target_os in ['linux']:
        out_dir = provisiontests_env.get('BUILD_DIR')
        result_dir = os.path.join(
            provisiontests_env.get('BUILD_DIR'), 'test_out') + os.sep
        if not os.path.isdir(result_dir):
            os.makedirs(result_dir)
        provisiontests_env.AppendENVPath('GTEST_OUTPUT', ['xml:' + result_dir])
        provisiontests_env.AppendENVPath('LD_LIBRARY_PATH', [out_dir])
        run_test(provisiontests_env,
                 'resource_provisioning_unittests_provisiontests.memcheck',
                 'resource/provisioning/unittests/provisiontests',
                  unittests)

src = File('#/resource/provisioning/examples/oic_svr_db_client.json')
dst = provisiontests_env.get('BUILD_DIR') + Dir('.').srcnode().path + "/" + src.name
i_n = provisiontests_env.Command(dst, src, Copy("$TARGET", "$SOURCE"))
provisiontests_env.Alias("install", i_n)
